$(function() {
	
	$("#users").click(function() {
		httpGet("/test/data/users", "#users");
	});
	
	$("#users_games").click(function() {
		var userId = $("#userId").val();
		httpGet("/test/data/users/" + userId + "/games", "#users_games");
	});
	
	$("#users_cardInstances").click(function() {
		var userId = $("#userId").val();
		httpGet("/test/data/users/" + userId + "/cardInstances", "#users_cardInstances");
	});
	
	$("#users_decks").click(function() {
		var userId = $("#userId").val();
		httpGet("/test/data/users/" + userId + "/decks", "#users_decks");
	});
	
	$("#games").click(function() {
		httpGet("/test/data/games", "#games");
	});
	
	$("#decks").click(function() {
		httpGet("/test/data/decks", "#decks");
	});
	
	$("#decks_cardInstances").click(function() {
		var deckId = $("#deckId").val();
		httpGet("/test/data/decks/" + deckId + "/cardInstances", "#decks_cardInstances");
	});
	
	$("#cards").click(function() {
		httpGet("/test/data/cards", "#cards");
	});
	
	$("#cards_cardInstances").click(function() {
		var cardId = $("#cardId").val();
		httpGet("/test/data/cards/" + cardId + "/cardInstances", "#cards_cardInstances");
	});
	
	$("#cardInstances").click(function() {
		httpGet("/test/data/cardInstances", "#cardInstances");
	});
	
	$("#properties").click(function() {
		httpGet("/test/data/properties", "#properties");
	});
	
	$("#otpGames").click(function() {
		httpGet("/test/data/otpGames", "#otpGames");
	});
	
});

function httpGet(url, countSelector) {
	$.ajax({
        type:"GET",
        beforeSend: function (request)
        {
            //request.setRequestHeader("Authority", authorizationToken);
        },
        url: url,
        data: null,
        processData: false,
        statusCode: {
        	404: function(response) {
        		if (response.responseText != undefined && response.responseText != null && response.responseText.length > 0) {
        			handleResponse( JSON.parse(response.responseText), countSelector );
        		}
        	}
        },
        success: function(response) {
        	handleResponse(response, countSelector);
        }
    });
}

function isArray(o) {
	return Object.prototype.toString.call(o) === '[object Array]';
}


function handleResponse(response, countSelector) {
	//var res = eval('(' + response + ')');
	$("#result").html( syntaxHighlight( JSON.stringify(response, undefined, 4) ) );
	
	if (response.d != undefined && response.d != null && isArray(response.d)) {
		var identifier = countSelector.substring(1);
		var out = identifier;
		identifier = identifier.split("_");
		if (identifier.length > 1) {
			out = identifier[1];
		}
		else {
			out = identifier[0];
		}
		
		$(countSelector).html(out + "(" + response.d.length + ")");
	}
}

function syntaxHighlight(json) {
	json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
	return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
	    var cls = 'number';
	    if (/^"/.test(match)) {
	        if (/:$/.test(match)) {
	            cls = 'key';
	        } else {
	            cls = 'string';
	        }
	    } else if (/true|false/.test(match)) {
	        cls = 'boolean';
	    } else if (/null/.test(match)) {
	        cls = 'null';
	    }
	    return '<span class="' + cls + '">' + match + '</span>';
	});
}